import 'package:flutter/material.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import '../../http/YZHttp.dart';
import '../../http/exception/AppException.dart';
import '../../http/exception/ExceptionConstant.dart';
import '../../widgets/LoadStateWidget.dart';
import 'BasicLogic.dart';

abstract class BasicListLogic<ListModel> extends BasicLogic {
  /// 刷新列表控制器
  final RefreshController refreshController = RefreshController();

  /// 列表数据
  var dataList = <ListModel>[];

  /// ScrollControler
  final ScrollController controller = ScrollController();

  /// 页数
  int page = 1;

  /// 第一次是否请求成功
  bool _isFirstRequestSuccess = false;

  /// 成功刷新
  void successRefresh(bool isHeaderRefresh,int endPageSize) {
    _isFirstRequestSuccess = true;
    if (isHeaderRefresh) {
      refreshController.refreshCompleted();
      refreshController.resetNoData();
      if (dataList.isEmpty) {
        pageState = LoadState.State_Empty;
      } else {
        pageState = LoadState.State_Success;
      }
    } else {
      if (endPageSize<PAGE_SIZE) {
        refreshController.loadNoData();
      } else {
        refreshController.loadComplete();
      }
    }
    update();
  }

  /// 失败刷新
  void failRefresh(bool isHeaderRefresh, Exception e) {
    print("e.toString() r${e.toString()}");
    if (isHeaderRefresh) {
      dataList = [];
      if (e is AppException) {
        if (e.getCode() == NODATA_ERROR_CODE) {
          pageState = LoadState.State_Empty;
        }
        // // 如果不是请求data = null，是其他错误
        else {
          pageState = LoadState.State_Error;
        }
      } else {
        pageState = LoadState.State_Error;
      }
      refreshController.refreshFailed();
    } else {
      refreshController.loadFailed();
    }
    update();
  }

  ///刷新控件底部retry方法
  void footerRetryRefresh();

  // 下啦刷新触发事件
  configRefresh() async {
    page = 1;
    getnetworkdata(true, configNetWorkParmas());
  }

  // 上啦加载更多触发事件
  configLoading() async {
    page++;
    getnetworkdata(false, configNetWorkParmas());
  }

  /// 网络请求 参数方法
  Map<String, dynamic>? configNetWorkParmas();

  /// 网络请求数据
  void getnetworkdata(bool isHeaderRefresh, Map<String, dynamic>? info);
}
